$(function () {
    obj.init();
});
var obj = new function () {
   'use strict';
    return {
        typeDataGrade:null,
        prodDataGrade:null,
        currentType:'',
        init:function () {
            this.typeDataGrade = new xhDataGrid($('#typeList'), 'ims/produce/getType');
            this.typeDataGrade.init().parseToolbar().activeButton([],['bt_type_del']);
            this.prodDataGrade = new xhDataGrid($('#prodList'), 'ims/produce/getProduce');
            this.prodDataGrade.init().parseToolbar().activeButton([],['bt_prod_del','bt_prod_accessory']);
            this.listener();
            this.funObj().bindTypeDgEvent();
            this.funObj().bindProdDgEvent();
        },
        listener:function () {
            var $this = this;
            //新增类型
            $('#bt_type_add').click(function () {
                $this.funObj().saveType();
            });
            //删除类型
            $('#bt_type_del').click(function () {
                if (xhExtra.buttonDisabled($(this))){
                    return false;
                }
                $this.funObj().delType();
            });
            //类型搜索
            $('#bt_type_search').click(function () {
               $this.funObj().searchType();
            });

            //新增商品
            $('#bt_prod_add').click(function () {
                $this.funObj().openProdForm();
            });
            //删除商品
            $('#bt_prod_del').click(function () {
                if (xhExtra.buttonDisabled($(this))) {
                    return false;
                }
                $this.funObj().delProd();
            });
            //商品搜索
            $('#bt_prod_search').click(function () {
                $this.funObj().searchProd();
            });
            //自定义商品
            $('#bt_prod_customProd').click(function () {
                $this.funObj().openCustomForm();
            });
            
            $('#bt_prod_accessory').click(function () {
                if (xhExtra.buttonDisabled($(this))) {
                    return false;
                }
                $this.funObj().openAccessoryForm();
            })
        },
        funObj:function () {
            var $this = this;
            return {
                saveType:function (row) {
                    window.top.swal({
                        titleText: row ? '修改类型' : '新增类型',
                        html:'<input type="text" class="form-control" id="name" placeholder="请输入类型名称" >',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        confirmButtonText: '确认',
                        showLoaderOnConfirm: true,
                        onOpen:function () {
                            if (row) {
                                $('#name', window.top.document).val(row['sType']);
                            }
                        },
                        preConfirm: function () {
                            return new Promise(function (resolve, reject) {
                                var name = $('#name', window.top.document).val();
                                if (!name) {
                                    reject('类型名称必填！');
                                } else {
                                    resolve(name);
                                }

                            })
                        },
                        allowOutsideClick: true
                    }).then(function (name) {
                        var params = {name:name};
                        if (row) {
                            params.id = row['iPTId'];
                        }
                        xhExtra.ajaxRequest('ims/produce/saveType', params, {}, function (res) {
                            window.top.xhTop.showSuccess(res.msg);
                            $this.typeDataGrade.reload();
                        });
                    }, function(dismiss) {});
                },
                delType:function () {
                    var id = $this.typeDataGrade.command('getChecked')[0]['iPTId'];
                    xhExtra.askChangeStatus('ims/produce/delType', {id:id}, $this.typeDataGrade, '确定要删除该商品类型吗？该分类下的商品也将删除',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                    })
                },
                bindTypeDgEvent:function () {
                    $this.typeDataGrade.onDblClickRow = function (rowIndex,rowData) {
                        $this.funObj().saveType(rowData);
                    };
                    $this.typeDataGrade.onCheck = function(){
                        $('#bt_type_del').setButtonEnable('eu');
                    };
                    $this.typeDataGrade.onUncheck = function(){
                        var _rows = $this.typeDataGrade.command('getChecked');
                        if(_rows.length==0){
                            $('#bt_type_del').setButtonDisable('eu');
                        }
                    };
                    $this.typeDataGrade.command({
                        onBeforeSelect:function(rowIndex,rowData){
                            if($this.currentType == rowData['iPTId']){
                                $this.currentType = '';
                                $this.prodDataGrade.reload({iPTId:0}).command('getPanel').panel('setTitle', '商品列表（全部）');
                                $this.typeDataGrade.clear();
                                return false;
                            }
                        }
                    });
                    $this.typeDataGrade.command({
                        onBeforeCheck:function(rowIndex,rowData){
                            $this.typeDataGrade.command('clearChecked');
                        }
                    });
                    $this.typeDataGrade.onSelect = function(rowIndex,rowData){
                        $this.currentType = rowData['iPTId'];
                        $this.prodDataGrade.reload({iPTId:$this.currentType}).command('getPanel').panel('setTitle', '商品列表（'+rowData['sType']+'）');
                    }
                },
                bindProdDgEvent:function () {
                    $this.prodDataGrade.onDblClickRow = function (rowIndex,rowData) {
                        if(rowData['iProdAttr'] === '普通商品') {
                            $this.funObj().openProdForm(rowData);
                        }else {
                            $this.funObj().openCustomForm(rowData);
                        }
                    };
                    $this.prodDataGrade.command({
                        view:detailview,
                        detailFormatter:function(index,row){
                            var str='<div class="ddv" style="padding:5px 0;overflow: auto;">';
                            if (row['extra'] != '') {
                                str += row['extra'];
                            }
                            str+='</div>';
                            return str;
                        },
                        onExpandRow: function(index,row){
                            var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
                            ddv.panel({
                                // height:200,
                                width:780,
                                border:false,
                                cache:false,
                                onLoad:function(){
                                    $this.prodDataGrade.command('fixDetailRowHeight',index);
                                    $this.prodDataGrade.command('fixDetailRowWidth',index);
                                }
                            });
                            $this.prodDataGrade.command('fixDetailRowHeight',index);
                        }
                    });
                    $this.prodDataGrade.onLoadSuccess = function (data) {
                        $.each(data['rows'],function (index,row) {
                            if(!row['extra']){
                                $this.prodDataGrade.command('getExpander',index).parent().hide().after('<div style="width: 24px;"></div>');
                            }
                        })
                    }
                },
                openProdForm:function (row) {
                    if (!row) {
                        window.top.xhTop.showModal({
                            title:'新增商品',
                            content: '<iframe src="'+baseUrl+'ims/produce/prodForm?currentType='+$this.currentType+'"  style="width: 100%;;border-style: none;padding: 0px;height: 300px;"  scrolling="yes" ></iframe>',
                            button:'提交',
                            backClose: false
                        });
                    } else {
                        window.top.xhTop.showModal({
                            title:'编辑商品',
                            content: '<iframe src="'+baseUrl+'ims/produce/prodForm?id='+row['iProdId']+'"  style="width: 100%;;border-style: none;padding: 0px;height: 300px;"  scrolling="yes" ></iframe>',
                            button:'提交',
                            backClose: false
                        });
                    }
                },
                delProd:function () {
                    var id = $this.prodDataGrade.getSelectValue('iProdId');
                    xhExtra.askChangeStatus('ims/produce/delProduce', {id:id}, $this.prodDataGrade, '确定要删除该商品吗？',function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                    })
                },
                searchType:function () {
                    var key = xhExtra.getValue($('#type_search'));
                    $this.typeDataGrade.reload({key:key});
                },
                searchProd:function () {
                    var $param = {
                        key:xhExtra.getValue($('#prod_search')),
                        attr:xhExtra.getValue($('#prod_attr')),
                        part:xhExtra.getValue($('#prod_part')),
                        brand:xhExtra.getValue($('#prod_brand'))
                    };
                    if ($this.currentType) {
                        $param.iPTId = $this.currentType;
                    }
                    $this.prodDataGrade.reload($param);
                },
                openCustomForm:function (row) {
                    if (!row) {
                        window.top.xhTop.showModal({
                            title:'新增自定义商品',
                            content: '<iframe src="'+baseUrl+'ims/produce/customProd?currentType='+$this.currentType+'"  style="width: 100%;;border-style: none;padding: 0px;height: 500px;"  scrolling="yes" ></iframe>',
                            button:'提交',
                            backClose: false
                        });
                    } else {
                        window.top.xhTop.showModal({
                            title:'编辑自定义商品',
                            content: '<iframe src="'+baseUrl+'ims/produce/customProd?id='+row['iProdId']+'"  style="width: 100%;;border-style: none;padding: 0px;height: 500px;"  scrolling="yes" ></iframe>',
                            button:'提交',
                            backClose: false
                        });
                    }
                },
                openAccessoryForm:function () {
                    var id = $this.prodDataGrade.getSelectValue('iProdId');
                    window.top.xhTop.showModal({
                        title:'配件列表',
                        content: '<iframe src="'+baseUrl+'ims/produce/accessoryForm?id='+id+'"  style="width: 100%;;border-style: none;padding: 0px;height: 500px;"  scrolling="yes" ></iframe>',
                        button:'提交',
                        backClose: false
                    });
                }
            }
        }
    }
};